package com.carrot.uaa.entity;

import com.baomidou.mybatisplus.annotation.TableName;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import lombok.Data;
import org.apache.commons.lang3.StringUtils;
import org.springframework.security.oauth2.core.AuthorizationGrantType;
import org.springframework.security.oauth2.core.ClientAuthenticationMethod;
import org.springframework.security.oauth2.server.authorization.settings.ClientSettings;
import org.springframework.security.oauth2.server.authorization.settings.TokenSettings;

import java.time.Instant;
import java.util.HashSet;
import java.util.Set;

/**
 * 注册表
 */
@Data
@JsonSerialize
@TableName("oauth2_registered_client")
public class OAuth2RegisteredClient {

    private String id;
    private String clientId;
    private Instant clientIdIssuedAt;
    private String clientSecret;
    private Instant clientSecretExpiresAt;
    private String clientName;
    private Set<ClientAuthenticationMethod> clientAuthenticationMethods;
    private Set<AuthorizationGrantType> authorizationGrantTypes;
    private Set<String> redirectUris;
    private Set<String> postLogoutRedirectUris;
    private Set<String> scopes;
    private ClientSettings clientSettings;
    private TokenSettings tokenSettings;

    public void setClientAuthenticationMethods(String clientAuthenticationMethodsStr) {
        this.clientAuthenticationMethods=new HashSet<>();
        if(StringUtils.isNotEmpty(clientAuthenticationMethodsStr)){
            String[] var = clientAuthenticationMethodsStr.split(",");
            for(int i=0;i<var.length;i++){
                ClientAuthenticationMethod clientAuthenticationMethod=new ClientAuthenticationMethod(var[i]);
                this.clientAuthenticationMethods.add(clientAuthenticationMethod) ;
            }
        }
    }

    public void setAuthorizationGrantTypes(String authorizationGrantTypesStr) {
        this.authorizationGrantTypes=new HashSet<>();
        if(StringUtils.isNotEmpty(authorizationGrantTypesStr)){
            String[] var = authorizationGrantTypesStr.split(",");
            for(int i=0;i<var.length;i++){
                AuthorizationGrantType authorizationGrantType=new AuthorizationGrantType(var[i]);
                this.authorizationGrantTypes.add(authorizationGrantType) ;
            }
        }
    }
}
